package ntd.util.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.log.StaticLog;
import ntd.util.UtilCatch;
import ntd.util.fun.ComFun;

public class ToolDuration {

    /**
     * 时长统计执行
     * @param emptyFun
     * @param name
     * @return
     * @param <T>
     */
    public static <T> T duration(ComFun.FunOut<T> emptyFun, String name) {
        StaticLog.info("----- {}执行开始 -----", name);
        T t = duration(emptyFun);
        StaticLog.info("----- {}执行结束 -----", name);
        return t;
    }

    public static <T> T duration(ComFun.FunOut<T> emptyFun) {
        TimeInterval timer = DateUtil.timer();
        T t = UtilCatch.tryCatch(emptyFun);
        StaticLog.info("----- 执行时长[{}]毫秒 -----", timer.interval());
        return t;
    }

    public static void duration(ComFun.Fun emptyFun, String name) {
        duration(() -> {
            emptyFun.accept();
            return null;
        }, name);
    }

    public static void duration(ComFun.Fun emptyFun) {
        duration(() -> {
            emptyFun.accept();
            return null;
        });
    }
}
